package com.klikmee.sms.core.base.communicator.mtel;

import java.text.MessageFormat;

import org.apache.log4j.Logger;

import hr.tis.mgw.client.message.DeliveryReport;
import hr.tis.mgw.client.message.ReceivedLongSmsSegment;
import hr.tis.mgw.client.message.ReceivedMms;
import hr.tis.mgw.client.message.ReceivedSms;
import hr.tis.mgw.client.message.notification.MessageListener2;

/**
 * Implementation of {@link MessageListener2} interface responsible for handling incoming message events.
 * 
 */
public class SimpleMessageListener implements MessageListener2 {
	/**
	 * Ref to the logger impl
	 */
	private Logger				logger				= Logger.getLogger( SimpleMessageListener.class );

	/**
	 * Reference to the mtel communicator implementation responsible for further message processing
	 */
	private MTelCommunicator	mTelCommunicator	= null;

	/**
	 * Invoked when delivery report is received. Forwards delivery report obj to mtel communicator
	 * 
	 * @param deliveryReport
	 *            delivery report to be processed.
	 */
	@Override
	public void deliveryReportReceived( DeliveryReport deliveryReport ) {
		// forward delivery report to the mtel communicator
		this.mTelCommunicator.onDeliveryReport( deliveryReport );
		logger.info( String.format( "Received delivery report for message id = [%s]", deliveryReport.getMessageId() ) );
	}

	@Override
	public void longSmsReceived( ReceivedLongSmsSegment arg0 ) {
		// TODO Auto-generated method stub
		logger.info( MessageFormat.format( "Received long sms part {0}", arg0.getSarSegmentSeqNum() ) );

	}

	@Override
	public void mmsReceived( ReceivedMms arg0 ) {
		// TODO Auto-generated method stub

	}

	/**
	 * Invoked on new SMS request. Forwards SMS message to the mtel communicator implementation for further handling.
	 * 
	 * @param smsMessage
	 *            actual sms message
	 */
	@Override
	public void smsReceived( ReceivedSms smsMessage ) {
		// forward message to the mtel communicator
		this.mTelCommunicator.onSmsMessage( smsMessage );
		logger.info( String.format( "Received new sms message from [%s]", smsMessage.getSenderAddress() ) );
	}

	public void setmTelCommunicator( MTelCommunicator mTelCommunicator ) {
		this.mTelCommunicator = mTelCommunicator;
	}
}
